package flc.ast.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.SeekBar;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import flc.ast.R;
import flc.ast.databinding.DialogPasswordViewBinding;
import flc.ast.databinding.DialogRandomNumberBinding;
import stark.common.basic.base.BaseSmartDialog;
import stark.common.basic.utils.Str2NumUtil;
import stark.common.basic.utils.ToastUtil;

public class RandomNumberDialog extends BaseSmartDialog<DialogRandomNumberBinding> implements View.OnClickListener {

    private int min = 1, max, count = 25;
    private boolean isUnique = true;

    public RandomNumberDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected int getGravity() {
        return Gravity.BOTTOM;
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_random_number;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivRandomOption.setOnClickListener(this);
        mDataBinding.tvGenerateRandomNumber.setOnClickListener(this);

        mDataBinding.sbRandomLength.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                count = progress;
                mDataBinding.tvRandomLength.setText(count + "");
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivCancel:
                dismiss();
                break;
            case R.id.ivRandomOption://随机数唯一
                clickOption();
                break;
            case R.id.tvGenerateRandomNumber://生成随机数
                generate();
                break;
            default:
                break;
        }
    }

    private void generate() {
        min = Str2NumUtil.parseInt(mDataBinding.etMin.getText().toString(), 0);
        max = Str2NumUtil.parseInt(mDataBinding.etMax.getText().toString(), 0);
        // 参数校验
        if (mDataBinding.etMin.getText().toString().isEmpty()) {
            ToastUtils.showShort(getContext().getString(R.string.please_input_min));
            return;
        }
        if (mDataBinding.etMax.getText().toString().isEmpty()) {
            ToastUtils.showShort(getContext().getString(R.string.please_input_max));
            return;
        }
        if (min > max) {
            ToastUtils.showShort(getContext().getString(R.string.min_not_greater_than_max));
            return;
        }
        if (isUnique && (max - min + 1) < count) {
            ToastUtils.showShort(getContext().getString(R.string.unique_not_random));
            return;
        }

        List<Integer> numbers = new ArrayList<>();
        if (isUnique) {
            // 生成唯一随机数：通过洗牌算法高效实现
            List<Integer> allNumbers = new ArrayList<>();
            for (int i = min; i <= max; i++) allNumbers.add(i);
            Collections.shuffle(allNumbers); // 打乱顺序
            numbers = allNumbers.subList(0, count); // 截取前 count 个
        } else {
            // 允许重复的随机数
            Random random = new Random();
            for (int i = 0; i < count; i++) {
                int num = random.nextInt((max - min) + 1) + min;
                numbers.add(num);
            }
        }
        String result = "";
        for (Integer number : numbers) {
            result += number + " ";
        }
        mDataBinding.tvRandomText.setText(result);
    }

    //随机数是否唯一
    private void clickOption() {
        if (isUnique) {
            isUnique = false;
            mDataBinding.ivRandomOption.setImageResource(R.drawable.guan);
        } else {
            isUnique = true;
            mDataBinding.ivRandomOption.setImageResource(R.drawable.kai);
        }
    }

}
